<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Attributes of Compound Components</title>
<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Spirit 2.5.8">
<link rel="up" href="../attributes.html" title="Attributes">
<link rel="prev" href="primitive_attributes.html" title="Attributes of Primitive Components">
<link rel="next" href="more_compound_attributes.html" title="More About Attributes of Compound Components">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="primitive_attributes.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../attributes.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="more_compound_attributes.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="spirit.abstracts.attributes.compound_attributes"></a><a class="link" href="compound_attributes.html" title="Attributes of Compound Components">Attributes
        of Compound Components</a>
</h4></div></div></div>
<p>
          <span class="emphasis"><em>Spirit.Qi</em></span> and <span class="emphasis"><em>Spirit.Karma</em></span> implement
          well defined attribute type propagation rules for all compound parsers
          and generators, such as sequences, alternatives, Kleene star, etc. The
          main attribute propagation rule for a sequences is for instance:
        </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    Library
                  </p>
                </th>
<th>
                  <p>
                    Sequence attribute propagation rule
                  </p>
                </th>
</tr></thead>
<tbody>
<tr>
<td>
                  <p>
                    Qi
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">a</span><span class="special">:</span>
                    <span class="identifier">A</span><span class="special">,</span>
                    <span class="identifier">b</span><span class="special">:</span>
                    <span class="identifier">B</span> <span class="special">--&gt;</span>
                    <span class="special">(</span><span class="identifier">a</span>
                    <span class="special">&gt;&gt;</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span></code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    Karma
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">a</span><span class="special">:</span>
                    <span class="identifier">A</span><span class="special">,</span>
                    <span class="identifier">b</span><span class="special">:</span>
                    <span class="identifier">B</span> <span class="special">--&gt;</span>
                    <span class="special">(</span><span class="identifier">a</span>
                    <span class="special">&lt;&lt;</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span></code>
                  </p>
                </td>
</tr>
</tbody>
</table></div>
<p>
          which reads as:
        </p>
<div class="blockquote"><blockquote class="blockquote"><p>
            Given <code class="computeroutput"><span class="identifier">a</span></code> and <code class="computeroutput"><span class="identifier">b</span></code> are parsers (generators), and <code class="computeroutput"><span class="identifier">A</span></code> is the attribute type of <code class="computeroutput"><span class="identifier">a</span></code>, and <code class="computeroutput"><span class="identifier">B</span></code>
            is the attribute type of <code class="computeroutput"><span class="identifier">b</span></code>,
            then the attribute type of <code class="computeroutput"><span class="identifier">a</span>
            <span class="special">&gt;&gt;</span> <span class="identifier">b</span></code>
            (<code class="computeroutput"><span class="identifier">a</span> <span class="special">&lt;&lt;</span>
            <span class="identifier">b</span></code>) will be <code class="computeroutput"><span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span></code>.
          </p></blockquote></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
            The notation <code class="computeroutput"><span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span></code>
            is used as a placeholder expression for any fusion sequence holding the
            types A and B, such as <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span></code> or <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span></code> (for more information see <a href="../../../../../../../libs/fusion/doc/html/index.html" target="_top">Boost.Fusion</a>).
          </p></td></tr>
</table></div>
<p>
          As you can see, in order for a type to be compatible with the attribute
          type of a compound expression it has to
        </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
              either be convertible to the attribute type,
            </li>
<li class="listitem">
              or it has to expose certain functionalities, i.e. it needs to conform
              to a concept compatible with the component.
            </li>
</ul></div>
<p>
          Each compound component implements its own set of attribute propagation
          rules. For a full list of how the different compound generators consume
          attributes see the sections <a class="link" href="../../qi/quick_reference/compound_attribute_rules.html" title="Compound Attribute Rules">Parser
          Compound Attribute Rules</a> and <a class="link" href="../../karma/quick_reference/compound_attribute_rules.html" title="Compound Attribute Rules">Generator
          Compound Attribute Rules</a>.
        </p>
<h6>
<a name="spirit.abstracts.attributes.compound_attributes.h0"></a>
          <span class="phrase"><a name="spirit.abstracts.attributes.compound_attributes.the_attribute_of_sequence_parsers_and_generators"></a></span><a class="link" href="compound_attributes.html#spirit.abstracts.attributes.compound_attributes.the_attribute_of_sequence_parsers_and_generators">The
          Attribute of Sequence Parsers and Generators</a>
        </h6>
<p>
          Sequences require an attribute type to expose the concept of a fusion sequence,
          where all elements of that fusion sequence have to be compatible with the
          corresponding element of the component sequence. For example, the expression:
        </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    Library
                  </p>
                </th>
<th>
                  <p>
                    Sequence expression
                  </p>
                </th>
</tr></thead>
<tbody>
<tr>
<td>
                  <p>
                    Qi
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">double_</span> <span class="special">&gt;&gt;</span>
                    <span class="identifier">double_</span></code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    Karma
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">double_</span> <span class="special">&lt;&lt;</span>
                    <span class="identifier">double_</span></code>
                  </p>
                </td>
</tr>
</tbody>
</table></div>
<p>
          is compatible with any fusion sequence holding two types, where both types
          have to be compatible with <code class="computeroutput"><span class="keyword">double</span></code>.
          The first element of the fusion sequence has to be compatible with the
          attribute of the first <code class="computeroutput"><span class="identifier">double_</span></code>,
          and the second element of the fusion sequence has to be compatible with
          the attribute of the second <code class="computeroutput"><span class="identifier">double_</span></code>.
          If we assume to have an instance of a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</span></code>, we can directly use the expressions
          above to do both, parse input to fill the attribute:
        </p>
<pre class="programlisting"><span class="comment">// the following parses "1.0 2.0" into a pair of double</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">input</span><span class="special">(</span><span class="string">"1.0 2.0"</span><span class="special">);</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">strbegin</span> <span class="special">=</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">p</span><span class="special">;</span>
<span class="identifier">qi</span><span class="special">::</span><span class="identifier">phrase_parse</span><span class="special">(</span><span class="identifier">strbegin</span><span class="special">,</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span>
    <span class="identifier">qi</span><span class="special">::</span><span class="identifier">double_</span> <span class="special">&gt;&gt;</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">double_</span><span class="special">,</span>       <span class="comment">// parser grammar </span>
    <span class="identifier">qi</span><span class="special">::</span><span class="identifier">space</span><span class="special">,</span>                        <span class="comment">// delimiter grammar</span>
    <span class="identifier">p</span><span class="special">);</span>                               <span class="comment">// attribute to fill while parsing</span>
</pre>
<p>
          and generate output for it:
        </p>
<pre class="programlisting"><span class="comment">// the following generates: "1.0 2.0" from the pair filled above</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">str</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">back_insert_iterator</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;</span> <span class="identifier">out</span><span class="special">(</span><span class="identifier">str</span><span class="special">);</span>
<span class="identifier">karma</span><span class="special">::</span><span class="identifier">generate_delimited</span><span class="special">(</span><span class="identifier">out</span><span class="special">,</span>
    <span class="identifier">karma</span><span class="special">::</span><span class="identifier">double_</span> <span class="special">&lt;&lt;</span> <span class="identifier">karma</span><span class="special">::</span><span class="identifier">double_</span><span class="special">,</span> <span class="comment">// generator grammar (format description)</span>
    <span class="identifier">karma</span><span class="special">::</span><span class="identifier">space</span><span class="special">,</span>                     <span class="comment">// delimiter grammar</span>
    <span class="identifier">p</span><span class="special">);</span>                               <span class="comment">// data to use as the attribute </span>
</pre>
<p>
          (where the <code class="computeroutput"><span class="identifier">karma</span><span class="special">::</span><span class="identifier">space</span></code> generator is used as the delimiter,
          allowing to automatically skip/insert delimiting spaces in between all
          primitives).
        </p>
<div class="tip"><table border="0" summary="Tip">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../images/tip.png"></td>
<th align="left">Tip</th>
</tr>
<tr><td align="left" valign="top">
<p>
            <span class="bold"><strong>For sequences only:</strong></span> <span class="emphasis"><em>Spirit.Qi</em></span>
            and <span class="emphasis"><em>Spirit.Karma</em></span> expose a set of API functions usable
            mainly with sequences. Very much like the functions of the <code class="computeroutput"><span class="identifier">scanf</span></code> and <code class="computeroutput"><span class="identifier">printf</span></code>
            families these functions allow to pass the attributes for each of the
            elements of the sequence separately. Using the corresponding overload
            of <span class="emphasis"><em>Qi's</em></span> parse or <span class="emphasis"><em>Karma's</em></span> <code class="computeroutput"><span class="identifier">generate</span><span class="special">()</span></code>
            the expression above could be rewritten as:
</p>
<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">d1</span> <span class="special">=</span> <span class="number">0.0</span><span class="special">,</span> <span class="identifier">d2</span> <span class="special">=</span> <span class="number">0.0</span><span class="special">;</span>
<span class="identifier">qi</span><span class="special">::</span><span class="identifier">phrase_parse</span><span class="special">(</span><span class="identifier">begin</span><span class="special">,</span> <span class="identifier">end</span><span class="special">,</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">double_</span> <span class="special">&gt;&gt;</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">double_</span><span class="special">,</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">space</span><span class="special">,</span> <span class="identifier">d1</span><span class="special">,</span> <span class="identifier">d2</span><span class="special">);</span>
<span class="identifier">karma</span><span class="special">::</span><span class="identifier">generate_delimited</span><span class="special">(</span><span class="identifier">out</span><span class="special">,</span> <span class="identifier">karma</span><span class="special">::</span><span class="identifier">double_</span> <span class="special">&lt;&lt;</span> <span class="identifier">karma</span><span class="special">::</span><span class="identifier">double_</span><span class="special">,</span> <span class="identifier">karma</span><span class="special">::</span><span class="identifier">space</span><span class="special">,</span> <span class="identifier">d1</span><span class="special">,</span> <span class="identifier">d2</span><span class="special">);</span>
</pre>
<p>
            where the first attribute is used for the first <code class="computeroutput"><span class="identifier">double_</span></code>,
            and the second attribute is used for the second <code class="computeroutput"><span class="identifier">double_</span></code>.
          </p>
</td></tr>
</table></div>
<h6>
<a name="spirit.abstracts.attributes.compound_attributes.h1"></a>
          <span class="phrase"><a name="spirit.abstracts.attributes.compound_attributes.the_attribute_of_alternative_parsers_and_generators"></a></span><a class="link" href="compound_attributes.html#spirit.abstracts.attributes.compound_attributes.the_attribute_of_alternative_parsers_and_generators">The
          Attribute of Alternative Parsers and Generators</a>
        </h6>
<p>
          Alternative parsers and generators are all about - well - alternatives.
          In order to store possibly different result (attribute) types from the
          different alternatives we use the data type <a href="http://www.boost.org/doc/html/variant.html" target="_top">Boost.Variant</a>.
          The main attribute propagation rule of these components is:
        </p>
<pre class="programlisting"><span class="identifier">a</span><span class="special">:</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">B</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">|</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">variant</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span>
</pre>
<p>
          Alternatives have a second very important attribute propagation rule:
        </p>
<pre class="programlisting"><span class="identifier">a</span><span class="special">:</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">A</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">|</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">A</span>
</pre>
<p>
          often allowing to simplify things significantly. If all sub expressions
          of an alternative expose the same attribute type, the overall alternative
          will expose exactly the same attribute type as well.
        </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="primitive_attributes.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../attributes.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="more_compound_attributes.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
